package com.example.demo.controller;

import com.example.demo.common.JsonResult;
import com.example.demo.entity.Type;
import com.example.demo.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @RequestMapping("getlist")
    public JsonResult getList() {
        return JsonResult.success(typeService.getList());
    }

    @RequestMapping("/admin/add")
    public JsonResult add(Type type) {
        if (!StringUtils.hasLength(type.getTypeName())) {
            return JsonResult.failure(-1, "非法参数");
        }
        return JsonResult.success(typeService.add(type));
    }

    @RequestMapping("/admin/delete")
    public JsonResult delete(Integer typeId) {
        if (typeId == null || typeId == 1) {
            return JsonResult.failure(-1, "非法参数");
        }
        return JsonResult.success(typeService.delete(typeId));
    }
}
